package com.yu.waterstation.app.moudles.worker.controller;

import com.yu.waterstation.common.entity.app.form.UpdatePasswordByWeiXinForm;
import com.yu.waterstation.common.entity.app.form.UpdatePasswordForm;
import com.yu.waterstation.common.entity.app.form.WeixinLoginForm;
import com.yu.waterstation.common.entity.app.dto.WorkerDTO;
import com.yu.waterstation.common.entity.app.form.UpdateWorkerForm;
import com.yu.waterstation.common.entity.app.form.WorkerLoginForm;
import com.yu.waterstation.app.moudles.worker.service.WorkerServiceImpl;
import com.yu.waterstation.common.common.annotation.NeedToken;
import com.yu.waterstation.common.common.annotation.VisitorAccess;
import com.yu.waterstation.common.common.enums.TokenType;
import com.yu.waterstation.common.common.util.result.Result;
import com.yu.waterstation.app.common.util.session.SessionUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

@Api(tags = "送水员")
@RestController
@RequestMapping("/worker")
public class WorkerController {
    @Resource
    private WorkerServiceImpl workerService;


    @ApiOperation("游客登录")
    @VisitorAccess
    @PostMapping("/login/guest")
    public Result<WorkerDTO> guestLogin() {
        WorkerDTO workerDTO = workerService.getWorkerInfo(0);
        if (workerDTO == null)
            return Result.fail("游客账号不存在");
        if (!workerDTO.getStatus())
            return Result.fail("游客账号已被冻结");
        SessionUtil.setWorkerSession(workerDTO); // 缓存会话信息
        return Result.ok(workerDTO);
    }


    @ApiOperation("通过手机和密码登录")
    @PostMapping("/login")
    public Result<WorkerDTO> login(@ApiParam("登录表单") @RequestBody WorkerLoginForm workerLoginVo) throws Exception {
        return workerService.login(workerLoginVo);
    }

    @ApiOperation("通过微信登录")
    @PostMapping("/login/wx")
    public Result<WorkerDTO> loginByWx(@RequestBody WeixinLoginForm weixinLoginForm) throws Exception {
        return workerService.loginByWx(weixinLoginForm);
    }

    @ApiOperation("退出登录")
    @VisitorAccess
    @NeedToken(TokenType.ENUM_worker_token)
    @DeleteMapping("/logout")
    public Result logout(HttpServletRequest request) {
        SessionUtil.workerLogout(request);
        return Result.ok();
    }

    @ApiOperation("获取用户数据")
    @VisitorAccess
    @NeedToken(TokenType.ENUM_worker_token)
    @GetMapping("/info")
    public Result<WorkerDTO> getWorkerInfo(HttpServletRequest request) {
        WorkerDTO workerDTO = workerService.getWorkerInfo(SessionUtil.getWorkerIdFromRequest(request));
        SessionUtil.setWorkerSession(workerDTO);
        return Result.ok(workerDTO);
    }

    @ApiOperation("绑定微信")
    @NeedToken(TokenType.ENUM_worker_token)
    @PutMapping("/weixin")
    public Result bindWeiXin(@RequestBody WeixinLoginForm form, HttpServletRequest request) throws Exception {
        return Result.ok(workerService.bindWeiXin(SessionUtil.getWorkerIdFromRequest(request), form));
    }

    @ApiOperation("更新密码")
    @NeedToken(TokenType.ENUM_worker_token)
    @PutMapping("/password")
    public Result updateStudent(@RequestBody UpdatePasswordForm vo, HttpServletRequest request) throws Exception {
        return Result.ok(workerService.updatePassword(SessionUtil.getWorkerIdFromRequest(request), vo));
    }

    @ApiOperation("微信授权修改密码")
    @NeedToken(TokenType.ENUM_worker_token)
    @PutMapping("/password/weixin")
    public Result updatePasswordByWeiXin(@RequestBody UpdatePasswordByWeiXinForm vo, HttpServletRequest request) throws Exception {
        return Result.ok(workerService.updatePasswordByWeiXin(SessionUtil.getWorkerIdFromRequest(request), vo));
    }

    @ApiOperation("更新信息")
    @NeedToken(TokenType.ENUM_worker_token)
    @PutMapping
    public Result<WorkerDTO> updateWorkerInfo(@RequestBody UpdateWorkerForm updateWorkerVo, HttpServletRequest request) throws Exception {
        return Result.ok(workerService.updateWorkerInfo(SessionUtil.getWorkerIdFromRequest(request), updateWorkerVo));
    }
}
